using MediatR;
using Microsoft.AspNetCore.Mvc;
using Medical.ErrorCode;
using BaseData.ErrorCode;
using Medical.ReadApi.Appcontext.Command;
using Medical.ReadApi.Dto;

namespace Medical.ReadApi.Controllers
{
	/// <summary>
	/// 员工管理控制器（读操作）
	/// </summary>
	[ApiController]
	[Route("api/[controller]/[action]")]
	public class EmployeeManagementController : ControllerBase
	{
		private readonly IMediator _mediator;
		
		/// <summary>
		/// 构造函数
		/// </summary>
		/// <param name="mediator">MediatR中介者</param>
		public EmployeeManagementController(IMediator mediator)
		{
			_mediator = mediator;
		}

		/// <summary>
		/// 根据ID获取员工详情
		/// </summary>
		/// <param name="employeeId">员工ID</param>
		/// <returns>员工详细信息</returns>
		/// <remarks>
		/// 示例请求:
		/// GET /api/EmployeeManagement/GetById?employeeId=1
		/// </remarks>
		/// <response code="200">查询成功，返回员工详细信息</response>
		/// <response code="400">请求参数错误</response>
		/// <response code="404">员工不存在</response>
		/// <response code="500">服务器内部错误</response>
		[HttpGet]
		[ProducesResponseType(typeof(ApiResult<EmployeeDto>), 200)]
		[ProducesResponseType(400)]
		[ProducesResponseType(404)]
		[ProducesResponseType(500)]
		public async Task<ApiResult<EmployeeDto>> GetById([FromQuery] long employeeId)
		{
			try { var data = await _mediator.Send(new GetEmployeeByIdQuery { EmployeeId = employeeId }); return new ApiResult<EmployeeDto> { Code = ApiEnum.查询成功, Message = "查询成功", data = data }; }
			catch (Exception ex) { return new ApiResult<EmployeeDto> { Code = ApiEnum.查询失败, Message = ex.Message, data = null }; }
		}

		/// <summary>
		/// 获取员工列表（支持分页和筛选）
		/// </summary>
		/// <param name="nameKeyword">员工姓名关键词（模糊搜索）</param>
		/// <param name="workNo">工号（精确匹配）</param>
		/// <param name="departmentId">科室ID筛选</param>
		/// <param name="role">角色筛选（如：医生、护士等）</param>
		/// <param name="page">页码（从1开始，默认值：1）</param>
		/// <param name="size">每页数量（默认值：10）</param>
		/// <returns>分页的员工列表</returns>
		/// <remarks>
		/// 示例请求:
		/// GET /api/EmployeeManagement/GetList?nameKeyword=张&amp;departmentId=1&amp;role=医生&amp;page=1&amp;size=10
		/// 
		/// 所有查询参数都是可选的，可以任意组合使用。
		/// </remarks>
		/// <response code="200">查询成功，返回分页的员工列表</response>
		/// <response code="400">请求参数错误</response>
		/// <response code="500">服务器内部错误</response>
		[HttpGet]
		[ProducesResponseType(typeof(ApiResult<ApiPaging<EmployeeDto>>), 200)]
		[ProducesResponseType(400)]
		[ProducesResponseType(500)]
		public async Task<ApiResult<ApiPaging<EmployeeDto>>> GetList([FromQuery] string? nameKeyword = null, [FromQuery] string? workNo = null, [FromQuery] long? departmentId = null, [FromQuery] string? role = null, [FromQuery] int page = 1, [FromQuery] int size = 10)
		{
			try { var data = await _mediator.Send(new GetEmployeesListQuery { NameKeyword = nameKeyword, WorkNo = workNo, DepartmentId = departmentId, Role = role, Page = page, Size = size }); return new ApiResult<ApiPaging<EmployeeDto>> { Code = ApiEnum.查询成功, Message = "查询成功", data = data }; }
			catch (Exception ex) { return new ApiResult<ApiPaging<EmployeeDto>> { Code = ApiEnum.查询失败, Message = ex.Message, data = null }; }
		}
	}
}
